import * as $group from '@/api/group'
import * as $unit from '@/api/unit'

import {toggleElFromArr} from '@/utils/helper'

import {
  UPDATE_FROM
} from '../user-group-create/store'

import {
  UPDATE_CURRENT_COURSE_ID
} from '../course-select/store'

export const UPDATE_GROUPS = 'update_groups'
export const UPDATE_GROUP_SELECT_IDS = 'update_group_select_ids'
export const PUSH_GROUPS = 'push_groups'
export const UNSHIRFT_GROUPS = 'unshirft_groups'
export const UPDATE_IS_PAGE_LOADED = 'update_is_page_loaded'
export const UPDATE_IS_SELECT_PANEL_SHOW = 'update_is_select_panel_show'
export const UPDATE_UNITS = 'update_units'
export const UPDATE_UNIT_DISPLAY_MODE = 'update_unit_display_mode'
export const UPDATE_UNIT_SELECT_IDS = 'update_unit_select_ids'
export const UPDATE_LESSON_SELECT_IDS = 'update_lesson_select_ids'

const state = () => {
  return {
    groups: [],
    units: [],
    groupSelectIds: [],
    isPageLoaded: false,
    isSelectPanelShow: true,
    unitDisplayMode: '',
    unitSelectIds: [],
    lessonSelectIds: [],
  }
}

const getters = {
  _groups (state) {
    let t = state.groups.map(n => {
      n.checked = state.groupSelectIds.indexOf(n.id) >= 0
      return n
    })
    return t
  },
  units (state) {
    let units = state.units.map(n => {
      let lessons = n.lessons || []
      lessons = lessons.map(nn => {
        let lesson = {
          id: nn.id,
          name: nn.name,
          checked: state.lessonSelectIds.includes(nn.id)
        }
        return lesson
      })
      let unit = {
        id: n.id,
        name: n.name,
        checked: state.unitSelectIds.includes(n.id),
        lessons
      }
      return unit
    })
    return units
  },
  exerciseSelectAble (state, rootState, getters, rootGetters) {
    if (state.groupSelectIds.length && (state.unitSelectIds.length || state.lessonSelectIds.length)) return true
    return false
  }
}

const actions = {
  async handleInit ({ commit, state, rootState }) {
    if (state.isPageLoaded) return
    let rs = await $group.getMys()
    commit(UPDATE_GROUPS, rs)
    commit(UPDATE_IS_PAGE_LOADED, true)
  },
  handleToggleCheckGroup ({ commit, state, rootState }, groupId) {
    let t = toggleElFromArr([...state.groupSelectIds], groupId)
    commit(UPDATE_GROUP_SELECT_IDS, t)
  },
  handleCreateGroup ({ commit, state, rootState }) {
    wx.navigateTo({
      url: '/pages/user-group-create/main?from=task-create-select-group'
    })
  },
  handleCourseSelect ({ commit, state, rootState }) {
    wx.navigateTo({
      url: '/pages/course-select/main'
    })
  },
  async fetchUnits ({ commit, state, rootState }, courseId) {
    let rs = await $unit.getUnitInfoByCourseId(courseId)
    commit(UPDATE_UNITS, rs.courseUnits)
    commit(UPDATE_UNIT_DISPLAY_MODE, rs.unitDisplayMode)
  },
  async handleToggleUnit ({ commit, state, rootState }, unitId) {
    let ids = toggleElFromArr([...state.unitSelectIds], unitId)
    commit(UPDATE_UNIT_SELECT_IDS, ids)
  },
  async handleToggleLesson ({ commit, state, rootState }, lessonId) {
    let ids = toggleElFromArr([...state.lessonSelectIds], lessonId)
    commit(UPDATE_LESSON_SELECT_IDS, ids)
  },
  handleSelectExercise ({ commit, state, rootState, getters }) {
    if (!getters.exerciseSelectAble) return
    wx.navigateTo({
      url: '/pages/exercise-select/main'
    })
  },
}

const mutations = {
  [UPDATE_GROUPS] (state, groups) {
    state.groups = groups
  },
  [UPDATE_GROUP_SELECT_IDS] (state, selectIds) {
    state.groupSelectIds = selectIds
  },
  [PUSH_GROUPS] (state, group) {
    state.groups = [...state.groups, group]
  },
  [UPDATE_IS_PAGE_LOADED] (state, flag) {
    state.isPageLoaded = flag
  },
  [UNSHIRFT_GROUPS] (state, group) {
    state.groups = [group, ...state.groups]
  },
  [UPDATE_IS_SELECT_PANEL_SHOW] (state, flag) {
    state.isSelectPanelShow = flag
  },
  [UPDATE_UNITS] (state, units) {
    state.units = units
  },
  [UPDATE_UNIT_DISPLAY_MODE] (state, mode) {
    state.unitDisplayMode = mode
  },
  [UPDATE_UNIT_SELECT_IDS] (state, ids) {
    state.unitSelectIds = ids
  },
  [UPDATE_LESSON_SELECT_IDS] (state, ids) {
    state.lessonSelectIds = ids
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
